package com.shopping.www.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shopping.www.model.Brand;
import com.shopping.www.model.BrandAndNum;
import com.shopping.www.model.BrandForDelivery;
import com.shopping.www.service.DeliveryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/delivery/*")
public class DeliveryServlet extends BaseServlet {
    private DeliveryService service=new DeliveryService();
    //查询要配送的商品
    public void selectDeliveryBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BrandAndNum<Brand> brandAndNum=service.selectDeliveryBrands();
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //接单
    public void takeOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String delivery_id=request.getParameter("delivery_id");
        delivery_id=new String(delivery_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _delivery_id=Integer.parseInt(delivery_id);
        BufferedReader br=request.getReader();
        String params=br.readLine();
        BrandForDelivery brandForDelivery=JSON.parseObject(params,BrandForDelivery.class);
        System.out.println(brandForDelivery);
        service.takeOrder(_delivery_id,brandForDelivery);
    }
    //查询骑手自己的订单（自己接的单）
    public void selectWaitDeliveryBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String delivery_id=request.getParameter("delivery_id");
        delivery_id=new String(delivery_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _delivery_id=Integer.parseInt(delivery_id);
        BrandAndNum<Brand> brandAndNum=service.selectWaitDeliveryBrands(_delivery_id);
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //在接单之前先判断是否已接下相同买家，相同商品的单,如果有，不能再次接，没有的话正常
    public void checkDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String delivery_id=request.getParameter("user_id");
        int _delivery_id=Integer.parseInt(delivery_id);
        String brand_id=request.getParameter("brand_id");
        int _brand_id=Integer.parseInt(brand_id);
        Boolean b=service.checkDelivery(_delivery_id,_brand_id);
        System.out.println(b);
        if(b){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("fail");
        }
    }
    //送达（配送完成）
    public void arriveDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String delivery_id=request.getParameter("delivery_id");
        delivery_id=new String(delivery_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _delivery_id=Integer.parseInt(delivery_id);
        BufferedReader br=request.getReader();
        String params=br.readLine();
        BrandForDelivery brandForDelivery=JSON.parseObject(params,BrandForDelivery.class);
        System.out.println(brandForDelivery);
        service.arriveDelivery(_delivery_id,brandForDelivery);
    }
}
